package cn.wanda.bi.sunshine.platform.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.wanda.bi.sunshine.platform.server.aspect.AccessLabel;
import cn.wanda.bi.sunshine.platform.server.aspect.AuthLabelList;
import cn.wanda.bi.sunshine.platform.server.aspect.AuthLabelSingle;
import cn.wanda.bi.sunshine.platform.server.aspect.CheckLimit;
import cn.wanda.bi.sunshine.platform.server.aspect.Logging;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelHitsDetail;
import cn.wanda.bi.sunshine.platform.server.controller.request.DimUserLabelAllsRequest;
import cn.wanda.bi.sunshine.platform.server.controller.request.LabelQueryRequest;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import cn.wanda.bi.sunshine.platform.server.service.LabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by wangfupeng1
 */
@Api(description = "标签服务")
@RestController
@RequestMapping(value = "/v1/label", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DMPLabelController implements Logging {

    public static final Logger logger = LoggerFactory.getLogger(DMPLabelController.class);

    @Autowired
    private LabelService dimUserLabelAllService;

    @CheckLimit
    @AccessLabel
	@AuthLabelList
    @ApiOperation("查询用户标签列表，输入是各种基础标签的查询条件组合列表，查询页码index和每页数据条数limit，应用的appid和secret，输出返回脱敏后满足条件的用户群信息(预先设定好的)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    ListResponse query(
            @ApiParam(value = "查询条件") @RequestBody LabelQueryRequest request,
            @ApiParam(value = "页码") @RequestParam(value = "index", required = false, defaultValue = "1") Integer index,
            @ApiParam(value = "每页个数") @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return dimUserLabelAllService.queryUserLabel(request, index, limit);
    }

    @AccessLabel
    @ApiOperation("查询人物画像，输入是各种基础标签的查询条件组合列表，应用的appid和secret，输出返回满足条件的用户群体所占比例")
    @RequestMapping(value = "/percent", method = RequestMethod.POST)
    SimpleResponse percent(
            @ApiParam(value = "查询条件") @RequestBody List<DimUserLabelAllsRequest> dimUserLabelAllsRequest,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return SimpleResponse.success(dimUserLabelAllService.queryDimUserLabelPicture(dimUserLabelAllsRequest));
    }

    @AccessLabel
    @ApiOperation("查询用户是否有标签，输入是各种基础标签的查询条件组合列表，应用的appid和secret，输出返回是否存在满足条件的用户，若存在，返回true，否则返回false")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    SimpleResponse check(
            @ApiParam(value = "查询条件") @RequestBody LabelQueryRequest request,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return dimUserLabelAllService.queryBoolean(request);
    }


	@AccessLabel
	@AuthLabelSingle
    @ApiOperation("查询用户标签，输入是各种基础标签的查询条件组合列表，应用的appid和secret，输出返回是否存在满足条件的用户，若存在，返回具体用户标签信息(预先设定好的)，否则返回用户标签信息为空")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    SimpleResponse<UserLabelHitsDetail> detail(
            @ApiParam(value = "查询条件") @RequestBody LabelQueryRequest request,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return dimUserLabelAllService.querySingle(request);
    }

	@AccessLabel
    @ApiOperation("查询标签分类数量，输入是所要查询的分类标签字段，以及查询条件，应用的appid和secret，输出是该分类下的数量")
    @RequestMapping(value = "/count/{field}", method = RequestMethod.POST)
    SimpleResponse countField(
            @ApiParam(value = "分类字段") @PathVariable String field,
            @ApiParam(value = "查询条件") @RequestBody LabelQueryRequest request,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return dimUserLabelAllService.countField(field, request);
    }

	@AccessLabel
    @ApiOperation("查询标签二层嵌套聚合分类数量，输入是所要查询的分类标签字段1,字段2，以及查询条件，应用的appid和secret，输出是该分类下的数量")
    @RequestMapping(value = "/count/{field1}/{field2}", method = RequestMethod.POST)
    SimpleResponse countField1Field2(
            @ApiParam(value = "分类字段1") @PathVariable String field1,
            @ApiParam(value = "分类字段2") @PathVariable String field2,
            @ApiParam(value = "查询条件") @RequestBody LabelQueryRequest request,
            @RequestHeader(value = "appid", defaultValue = "ss8f239bb5b209c060") String appid,
            @RequestHeader(value = "secret", defaultValue = "eaf109772eec37a5663b6659fbe5c56a") String secret) {
        return dimUserLabelAllService.countField1Field2(field1,field2, request);
    }


}
